﻿
from collections import Iterable

L=['Michael','Sarah','Tracy','Bob','Jack']
print "L =",L

print "L[0],L[1],L[2] =",L[0],L[1],L[2]

print "L[0:3] =",L[0:3]
print "L[:3] =",L[:3]
print "L[1:3] =",L[1:3]
print "L[-2:-1] =",L[-2:-1]
print "L[:] =",L[:]

print "(1,2,3,4,5,6)[:3] =",(1,2,3,4,5,6)[:3]

print "'ABCDEFT'[:3] =",'ABCDEFT'[:3]

print "-------------------------------------------iteration-------------------------------------------"
print "L =",L

for n in L:
	print "for in list -->",n

print "----"

M={'a':1,'b':2,'c':3,'d':4}
print "M =",M

for key in M:
	print "for in dict --->",key

ch='ABC'
print "ch =",ch

for c in ch:
	print "for in string -->",c
	
# 那么，如何判断一个对象是可迭代对象呢？方法是通过collections模块的Iterable类型判断：

print "isinstance('abc',Iterable) =",isinstance('abc',Iterable)
print "isinstance([1,2,3],Iterable) =",isinstance([1,2,3],Iterable)
print "isinstance(123,Iterable) =",isinstance(123,Iterable)

# 如果要对list实现类似Java那样的下标循环怎么办？
# Python内置的enumerate函数可以把一个list变成索引-元素对，这样就可以在for循环中同时迭代索引和元素本身：
for i,value in enumerate(['A','B','C']):
	print "for in enumerate -->",i,value
	
for x,y in [(1,1),(2,4),(3,9)]:
	print "for of 2 variables -->",x,y